package com.lq.learningTest.jihe;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;

public class IteratorSetTest {
    public static void main(String[] args) throws InterruptedException {

//        ArrayList<String> list = new ArrayList<>();
//        Collections.addAll(list,"asd","erta","tara","gatre","sdgt");
////        for (String str : list) {
////            System.out.println(str);
////        }
////        Iterator<String> iterator = list.iterator();
////        while (iterator.hasNext()) {
////            String next = iterator.next();
////            System.out.println(next);
////
////        }
//        Iterator<String> iterator = list.iterator();
////        for (String s : list) {
////            if (s.equals("tara")) {
//////                iterator.remove();
////                list.remove(s);
////            }
////        }
//
//        while (iterator.hasNext()) {
//            if (iterator.next().equals("tara")) {
//                iterator.remove();
////                list.remove("tara");
//            }
//        }
//        System.out.println(list);



//    Integer[] arr = new Integer[]{1,2,3,4,5,6,7,8,9,10};
//    for (Integer i : arr) {
//        System.out.println(i);
//    }
//        //HashSet
//        Set<String> set = new HashSet<>();
//        set.add("j");
//        set.add("b");
//        set.add("k");
//        set.add("d");
//        set.add("l");
//        set.add("i");
//        set.add("m");
//        set.add("n");
//        set.add("o");
//        set.add("p");
//        Iterator<String> iterator = set.iterator();
//        while (iterator.hasNext()) {
//            String next = iterator.next();
//            System.out.println(next);
//        }
//        for (String s : set) {
//            System.out.println(s);
//        }

        //hashmap遍历第一种
//        Map<Integer, String> hashmap = new HashMap<>();
//        for (int i = 0; i < 100; i++) {
//            hashmap.put(i,"lq");
//        }
//        Set<Integer> integers = hashmap.keySet();
//        Iterator<Integer> iterator = integers.iterator();
//        while (iterator.hasNext()) {
//            Integer next = iterator.next();
//            System.out.print("key:    "+next+", ");
//            System.out.println("value:  "+hashmap.get(next));
//        }

        //hashmap遍历第二种，entry是键值对,就是把每个key和对应value打包为一个entry实例对象,放到set里面
//        Set<Map.Entry<Integer, String>> entries = hashmap.entrySet();
//        Iterator<Map.Entry<Integer, String>> iterator2 = entries.iterator();
//        while (iterator2.hasNext()) {
//            Map.Entry<Integer, String> next = iterator2.next();
//            System.out.println(next.getKey()+":"+next.getValue());

            //错误示范，调用了两次next迭代，会导致缺少内容
//            System.out.println(iterator2.next().getKey());
//            System.out.println(iterator2.next().getValue());

        //第三种增强for循环
//        for (Map.Entry<Integer, String> entry : entries) {
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());

//        }
        //共享资源
//        final ArrayList<Integer> list = new ArrayList<>();
//        Vector<Integer> list = new Vector<>();
        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 200; i++) {
            if(new Random().nextInt(10) > 5){
                new Thread(()->{
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    list.add(1);
                }).start();
            }
            else {

            }

        }

    }
}
